package com.ahdy.service.impl;

import com.ahdy.entity.ElderlyUser;
import com.ahdy.mapper.ElderlyUserMapper;
import com.ahdy.service.ElderlyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 老人用户服务实现类
 */
@Service
public class ElderlyUserServiceImpl implements ElderlyUserService {

    @Autowired
    private ElderlyUserMapper elderlyUserMapper;

    @Override
    @Transactional
    public Integer addElderlyUser(ElderlyUser elderlyUser) {
        elderlyUserMapper.insert(elderlyUser);
        return elderlyUser.getId();
    }

    @Override
    @Transactional
    public int updateElderlyUser(ElderlyUser elderlyUser) {
        return elderlyUserMapper.update(elderlyUser);
    }

    @Override
    @Transactional
    public int deleteElderlyUser(Integer id) {
        return elderlyUserMapper.deleteById(id);
    }

    @Override
    public ElderlyUser getElderlyUserById(Integer id) {
        return elderlyUserMapper.findById(id);
    }

    @Override
    public List<ElderlyUser> getAllElderlyUsers() {
        return elderlyUserMapper.findAll();
    }

    @Override
    public ElderlyUser getElderlyUserByPhone(String phone) {
        return elderlyUserMapper.findByPhone(phone);
    }

    @Override
    public ElderlyUser login(String phone, String password) {
        return elderlyUserMapper.findByPhoneAndPassword(phone, password);
    }

    @Override
    @Transactional
    public int updateLastLogin(Integer id, LocalDateTime lastLogin) {
        return elderlyUserMapper.updateLastLogin(id, lastLogin);
    }

    @Override
    public List<ElderlyUser> getElderlyUsersByStatus(ElderlyUser.UserStatus status) {
        return elderlyUserMapper.findByStatus(status);
    }

    @Override
    public List<ElderlyUser> getElderlyUsersByFamilyId(Integer familyId) {
        return elderlyUserMapper.findElderlyByFamilyId(familyId);
    }

    @Override
    public int countActiveUsers() {
        return elderlyUserMapper.countActiveUsers();
    }
}